package com.huabo.system.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.hbfk.entity.*;
import com.hbfk.entity.TblAttachment;
import com.hbfk.util.*;
import com.huabo.system.config.DateBaseConfig;
import com.huabo.system.mysql.entity.*;
import com.huabo.system.mysql.mapper.TblLoginTypeMySqlMapper;
import com.huabo.system.mysql.mapper.TblOrganizationMySqlMapper;
import com.huabo.system.mysql.mapper.TblStaffMySqlMapper;
import com.huabo.system.oracle.entity.*;
import com.huabo.system.oracle.mapper.TblAttachmentMapper;
import com.huabo.system.oracle.mapper.TblLoginTypeMapper;
import com.huabo.system.oracle.mapper.TblOrganizationMapper;
import com.huabo.system.oracle.mapper.TblPersonalTrainMapper;
import com.huabo.system.oracle.mapper.TblStaffMapper;
import com.huabo.system.service.TblStaffService;
import com.huabo.system.utils.MD5Encrypt;

import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;

import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


@Service("tblStaffServiceImpl")
@Slf4j
public class TblStaffServiceImpl implements TblStaffService {

    @Resource
    public TblStaffMapper tblStaffMapper;
    @Resource
    public TblStaffMySqlMapper tblStaffMySqlMapper;

    @Resource
    public TblOrganizationMapper tblOrganizationMapper;

    @Resource
    public TblOrganizationMySqlMapper tblOrganizationMySqlMapper;

    @Resource
    public TblLoginTypeMapper tblLogintypeMapper;

    @Resource
    public TblLoginTypeMySqlMapper tblLoginTypeMySqlMapper;
    
    @Resource
    public TblPersonalTrainMapper tblPersonalTrainMapper;
    
    @Resource
   	private TblAttachmentMapper tblAttachmentMapper;

    public JsonBean dealLoginSystem(String userName, String password, String loginType, String verid)
            throws Exception {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            Jedis jedis = null;
            Map<String, Object> datanMap = new HashMap<String, Object>(0);
            try {
                jedis = JedisUtil.getJedis();
//			if(!"9999".equals(verifyCode)) {
//				String verCode = jedis.get(verid);
//				if(verid == null || "".equals(verid) || verCode == null || "".equals(verCode)) {
//					log.info(userName+"用户登录验证码已过期");
//					return ResponseFormat.retParam(0,10005,null);
//				}
//				if(!verifyCode.equals(verCode)) {
//					log.info(userName+"用户登录验证码错误");
//					return ResponseFormat.retParam(0,10006,null);
//				}
//			}
                log.info("根据用户名密码 获取用户" + userName + "信息");
                TblStaff staff = this.tblStaffMapper.selectUniqueStaffInfo(userName, Md5util.encrypByMd5(password));
                if (staff == null) {
                    log.info(userName + "用户名或密码错误！");
                    return ResponseFormat.retParam(0, 20002, null);
                }

                TblStaffUtil staffUtil = new TblStaffUtil();
                staffUtil.setStaffid(staff.getStaffid());
                staffUtil.setRealname(staff.getRealname());
                staffUtil.setUsername(staff.getUsername());
                staffUtil.setAddress(staff.getAddress());
                staffUtil.setEmail(staff.getEmail());
                staffUtil.setMiblephone(staff.getMiblephone());
                staffUtil.setMemo(staff.getMemo());
                staffUtil.setStatus(staff.getStatus());
                staffUtil.setCreateDate(staff.getCreateDate());
                staffUtil.setJobid(staff.getJobid());
                staffUtil.setOutSideId(staff.getOutSideId());
                staffUtil.setOutSideOpenId(staff.getOutSideOpenId());
                staffUtil.setFixedphone(staff.getFixedphone());
                staffUtil.setRoleIdStrs(staff.getRoleIdStrs());
                staffUtil.setRoleNames(staff.getRoleNames());
                staffUtil.setPkYmStaffId(staff.getPkYmStaffId());
                if (staff.getTrole() != null) {
                    TblRoleUtil roleUtil = new TblRoleUtil();
                    roleUtil.setRid(staff.getTrole().getRid());
                    roleUtil.setRname(staff.getTrole().getRname());
                    staffUtil.setTrole(roleUtil);
                }
                if (staff.getLinkDetp() != null) {
                    TblOrganizationUtil deptUtil = new TblOrganizationUtil();
                    deptUtil.setOrgid(staff.getLinkDetp().getOrgid());
                    deptUtil.setOrgname(staff.getLinkDetp().getOrgname());
                    staffUtil.setLinkDetp(deptUtil);
                }

                TblOrganization linkOrg = this.tblOrganizationMapper.selectCompanyInfoByDeptId(staff.getLinkDetp().getOrgid());

                TblOrganizationUtil orgUtil = new TblOrganizationUtil();
                orgUtil.setOrgid(linkOrg.getOrgid());
                orgUtil.setOrgname(linkOrg.getOrgname());
                orgUtil.setFatherorgid(linkOrg.getFatherorgid());
                orgUtil.setOrgnumber(linkOrg.getOrgnumber());
                orgUtil.setOrgmeno(linkOrg.getOrgmeno());
                orgUtil.setMemo(linkOrg.getMemo());
                orgUtil.setIcode(linkOrg.getIcode());
                orgUtil.setOrgtype(linkOrg.getOrgtype());
                orgUtil.setStatus(linkOrg.getStatus());
                orgUtil.setIszy(linkOrg.getIszy());
                staffUtil.setLinkOrg(orgUtil);
                staffUtil.setCurrentOrg(orgUtil);

//                TblLoginType logintype = this.tblLogintypeMapper.selectLoginTypeInfo("/" + loginType);
//
//                if (logintype != null) {
//                    TblLoginTypeUtil typeUtil = new TblLoginTypeUtil();
//                    typeUtil.setLoginid(logintype.getLoginid());
//                    staffUtil.setLoginType(typeUtil);
//                }
                String token = EncryptUtil.getInstance().AESencode(staff.getStaffid().toString(), EncryptUtil.DESKEY);

                jedis.set(staff.getStaffid() + JedisUtil.USERINFOKEY, JSONObject.toJSONString(staffUtil));
                datanMap.put("token", token);
                log.info("用户登录成功，返回用户信息、token: " + token);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (verid != null) {
                    jedis.del(verid);
                }
                if (jedis != null) {
                    jedis.close();
                }
            }
            return ResponseFormat.retParam(1, 200, datanMap);
        } else {
            Jedis jedis = null;
            Map<String, Object> datanMap = new HashMap<String, Object>(0);
            try {
                jedis = JedisUtil.getJedis();
//			if(!"9999".equals(verifyCode)) {
//				String verCode = jedis.get(verid);
//				if(verid == null || "".equals(verid) || verCode == null || "".equals(verCode)) {
//					log.info(userName+"用户登录验证码已过期");
//					return ResponseFormat.retParam(0,10005,null);
//				}
//				if(!verifyCode.equals(verCode)) {
//					log.info(userName+"用户登录验证码错误");
//					return ResponseFormat.retParam(0,10006,null);
//				}
//			}
                log.info("根据用户名密码 获取用户" + userName + "信息");
                TblStaffMySql staff = this.tblStaffMySqlMapper.selectUniqueStaffInfo(userName, Md5util.encrypByMd5(password));
                if (staff == null) {
                    log.info(userName + "用户名或密码错误！");
                    return ResponseFormat.retParam(0, 20002, null);
                }

                TblStaffUtil staffUtil = new TblStaffUtil();
                staffUtil.setStaffid(staff.getStaffid());
                staffUtil.setRealname(staff.getRealname());
                staffUtil.setUsername(staff.getUsername());
                staffUtil.setAddress(staff.getAddress());
                staffUtil.setEmail(staff.getEmail());
                staffUtil.setMiblephone(staff.getMiblephone());
                staffUtil.setMemo(staff.getMemo());
                staffUtil.setStatus(staff.getStatus());
                staffUtil.setCreateDate(staff.getCreateDate());
                staffUtil.setJobid(staff.getJobid());
                staffUtil.setOutSideId(staff.getOutSideId());
                staffUtil.setOutSideOpenId(staff.getOutSideOpenId());
                staffUtil.setFixedphone(staff.getFixedphone());
                staffUtil.setRoleIdStrs(staff.getRoleIdStrs());
                staffUtil.setRoleNames(staff.getRoleNames());
                staffUtil.setPkYmStaffId(staff.getPkYmStaffId());
                if (staff.getTrole() != null) {
                    TblRoleUtil roleUtil = new TblRoleUtil();
                    roleUtil.setRid(staff.getTrole().getRid());
                    roleUtil.setRname(staff.getTrole().getRname());
                    staffUtil.setTrole(roleUtil);
                }
                if (staff.getLinkDetp() != null) {
                    TblOrganizationUtil deptUtil = new TblOrganizationUtil();
                    deptUtil.setOrgid(staff.getLinkDetp().getOrgid());
                    deptUtil.setOrgname(staff.getLinkDetp().getOrgname());
                    staffUtil.setLinkDetp(deptUtil);
                }

                TblOrganizationMySql linkOrg = this.tblOrganizationMySqlMapper.selectCompanyInfoByDeptId(staff.getLinkDetp().getOrgid());

                TblOrganizationUtil orgUtil = new TblOrganizationUtil();
                orgUtil.setOrgid(linkOrg.getOrgid());
                orgUtil.setOrgname(linkOrg.getOrgname());
                orgUtil.setFatherorgid(linkOrg.getFatherorgid());
                orgUtil.setOrgnumber(linkOrg.getOrgnumber());
                orgUtil.setOrgmeno(linkOrg.getOrgmeno());
                orgUtil.setMemo(linkOrg.getMemo());
                orgUtil.setIcode(linkOrg.getIcode());
                orgUtil.setOrgtype(linkOrg.getOrgtype());
                orgUtil.setStatus(linkOrg.getStatus());
                orgUtil.setIszy(linkOrg.getIszy());
                staffUtil.setLinkOrg(orgUtil);
                staffUtil.setCurrentOrg(orgUtil);

                TblLoginTypeMySql logintype = this.tblLoginTypeMySqlMapper.selectLoginTypeInfo("/" + loginType);

                if (logintype != null) {
                    TblLoginTypeUtil typeUtil = new TblLoginTypeUtil();
                    typeUtil.setLoginid(logintype.getLoginid());
                    staffUtil.setLoginType(typeUtil);
                }
                String token = EncryptUtil.getInstance().AESencode(staff.getStaffid().toString(), EncryptUtil.DESKEY);

                jedis.set(staff.getStaffid() + JedisUtil.USERINFOKEY, JSONObject.toJSONString(staffUtil));
                datanMap.put("token", token);
                log.info("用户登录成功，返回用户信息、token: " + token);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (verid != null) {
                    jedis.del(verid);
                }
                if (jedis != null) {
                    jedis.close();
                }
            }
            return ResponseFormat.retParam(1, 200, datanMap);
        }
    }

    @Override
    public boolean validateOrgExistStaff(List<TblOrganization> findChildrenByOrgid) {
        return tblStaffMapper.selectFindChildrenByOrgid(findChildrenByOrgid);
    }

    @Override
    public boolean validateOrgExistMySqlStaff(List<TblOrganizationMySql> findChildrenByOrgid) {
        return tblStaffMySqlMapper.selectFindChildrenByOrgid(findChildrenByOrgid);
    }

    @Override
    public TblStaff get(BigDecimal pid) {
        return tblStaffMapper.selectUserId(pid);
    }

    @Override
    public TblStaffMySql getMySql(BigDecimal pid) {
        return tblStaffMySqlMapper.selectUserId(pid);
    }

    @Override
    public TblStaff findByUsername(String username) {
        return tblStaffMapper.selectUserName(username);
    }

    @Override
    public TblStaffMySql findByMySqlUsername(String username) {
        return tblStaffMySqlMapper.selectUserName(username);
    }

    @Override
    public Map<String, Object> add(TblStaff user) {
        String result = null;
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        tblStaffMapper.insertUser(user);
        resultMap.put("code", "1");
        resultMap.put("msg", "访问接口成功");
        return resultMap;
    }

    @Override
    public Map<String, Object> addMySql(TblStaffMySql user) {
        String result = null;
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        tblStaffMySqlMapper.insertUser(user);
        resultMap.put("code", "1");
        resultMap.put("msg", "访问接口成功");
        return resultMap;
    }

    @Override
    public List<TblStaff> findByEmail(String email) {
        return tblStaffMapper.selectEmail(email);
    }

    @Override
    public List<TblStaffMySql> findByMySqlEmail(String email) {
        return tblStaffMySqlMapper.selectEmail(email);
    }

    @Override
    public void modify(TblStaff user) {
        tblStaffMapper.updateUser(user);
    }

    @Override
    public void modifyMySql(TblStaffMySql user) {
        tblStaffMySqlMapper.updateUser(user);
    }

    @Override
    public TblStaff findById(String id) {
        return tblStaffMapper.selectPid(id);
    }

    @Override
    public TblStaffMySql findByMySqlId(String id) {
        return tblStaffMySqlMapper.selectPid(id);
    }

    //权限
    @Override
    public Map<String, Object> findAll(String staffId, Find find, Integer pageNumber, Integer pageSize, BigDecimal pid) {
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        try {
            PageInfo<TblStaff> pageInfo = new PageInfo<TblStaff>();
            pageInfo.setCurrentPage(pageNumber);
            pageInfo.setPageSize(pageSize);

            pageInfo.setTlist(tblStaffMapper.selectListByPageIn(pageInfo, pid, find));
            pageInfo.setTotalRecord(tblStaffMapper.selectListByPageInCount(pageInfo, pid));
            resultMap.put("code", "1");
            resultMap.put("msg", "访问接口成功");
            resultMap.put("data", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> findAllMySql(String staffId, FindMySql find, Integer pageNumber, Integer pageSize, BigDecimal pid) {
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        try {
            PageInfo<TblStaffMySql> pageInfo = new PageInfo<TblStaffMySql>();
            pageInfo.setCurrentPage(pageNumber);
            pageInfo.setPageSize(pageSize);

            pageInfo.setTlist(tblStaffMySqlMapper.selectListByPageIn(pageInfo, pid, find));
            pageInfo.setTotalRecord(tblStaffMySqlMapper.selectListByPageInCount(pageInfo, pid));
            resultMap.put("code", "1");
            resultMap.put("msg", "访问接口成功");
            resultMap.put("data", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> findAllPid(String staffId, Find find, Integer pageNumber, Integer pageSize, String token) {
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        try {
            PageInfo<TblStaff> pageInfo = new PageInfo<TblStaff>();
            TblStaffUtil staff = DealUserToken.parseUserToken(token);
            BigDecimal orgid = staff.getCurrentOrg().getOrgid();
            pageInfo.setCurrentPage(pageNumber);
            pageInfo.setPageSize(pageSize);

            pageInfo.setTlist(tblStaffMapper.selectListByPageInfoo(pageInfo, find, orgid));
            pageInfo.setTotalRecord(tblStaffMapper.selectCountFind(pageInfo, orgid));
            resultMap.put("code", "1");
            resultMap.put("msg", "访问接口成功");
            resultMap.put("data", pageInfo);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> findAllMySqlPid(String staffId, FindMySql find, Integer pageNumber, Integer pageSize, String token) {
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        try {
            PageInfo<TblStaffMySql> pageInfo = new PageInfo<TblStaffMySql>();
            TblStaffUtil staff = DealUserToken.parseUserToken(token);
            BigDecimal orgid = staff.getCurrentOrg().getOrgid();
            pageInfo.setCurrentPage(pageNumber);
            pageInfo.setPageSize(pageSize);

            pageInfo.setTlist(tblStaffMySqlMapper.selectListByPageInfoo(pageInfo, find, orgid));
            pageInfo.setTotalRecord(tblStaffMySqlMapper.selectCountFind(pageInfo, orgid));
            resultMap.put("code", "1");
            resultMap.put("msg", "访问接口成功");
            resultMap.put("data", pageInfo);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return resultMap;
    }

    @Override
    public TblStaff findByStaffid(BigDecimal staffid) throws Exception {
		if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
			TblStaff tblStaff = tblStaffMapper.findByStaffid(staffid);
			
			//==角色
			String roleids = tblStaff.getRoleIdStrs();
			if(null!=roleids && !"".equals(roleids)) {
				String roleNames = "";
				List<TblRole> listOrg = this.tblStaffMapper.selectListRoleInId(roleids);
				if(null != listOrg) {
					for (int i = 0; i < listOrg.size(); i++) {
						TblRole org = listOrg.get(i);
						String orgname = org.getRname();
						roleNames += orgname;
						if((listOrg.size()-1) > i) {
							roleNames += ",";
						}
					}
				}
				tblStaff.setRoleNames(roleNames);
			}
			
//			//==
//			String roleids = tblStaff.getRoleIdStrs();
//			if(null!=roleids && !"".equals(roleids)) {
//				String roleNames = "";
//				List<TblRole> listOrg = this.tblStaffMapper.selectListRoleInId(roleids);
//				if(null != listOrg) {
//					for (int i = 0; i < listOrg.size(); i++) {
//						TblRole org = listOrg.get(i);
//						String orgname = org.getRname();
//						roleNames += orgname;
//						if((listOrg.size()-1) > i) {
//							roleNames += ",";
//						}
//					}
//				}
//				tblStaff.setRoleNames(roleNames);
//			}
			
			return tblStaff;
		} else {
			TblStaff tblStaff = tblStaffMapper.findByStaffid(staffid);
			return tblStaff;
		}
    }

    @Override
    public TblStaffMySql findByMySqlStaffid(BigDecimal staffid) {
        TblStaffMySql tblStaff = tblStaffMySqlMapper.findByStaffid(staffid);
        return tblStaff;
    }

    @Override
    public Map<String, Object> update(TblStaff user) {
        String result = null;
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        tblStaffMapper.updateStaff(user);
        resultMap.put("msg", "访问接口成功");
        resultMap.put("code", 1);
        return resultMap;
    }

    @Override
    public Map<String, Object> updateMySql(TblStaffMySql user) {
        String result = null;
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        tblStaffMySqlMapper.updateStaff(user);
        resultMap.put("msg", "访问接口成功");
        resultMap.put("code", 1);
        return resultMap;
    }

    @Override
    public Map<String, Object> findByAll(String pid, Integer pageNumber, Integer pageSize, String token, String staffId) {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            try {
                TblStaffUtil user = DealUserToken.parseUserToken(token);
                BigDecimal orgid = user.getCurrentOrg().getOrgid();
                PageInfo<TblStaff> pageInfo = new PageInfo<TblStaff>();
                pageInfo.setCurrentPage(pageNumber);
                pageInfo.setPageSize(pageSize);
                pageInfo.setTlist(tblStaffMapper.selectListByPage(pageInfo, pid));
                pageInfo.setTotalRecord(tblStaffMapper.selectListByPageCount(pageInfo, pid));
                resultMap.put("code", "1");
                resultMap.put("msg", "访问接口成功");
                resultMap.put("data", pageInfo);
                //resultMap.put("data",orgid);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return resultMap;
        } else {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            try {
                TblStaffUtil user = DealUserToken.parseUserToken(token);
                BigDecimal orgid = user.getCurrentOrg().getOrgid();
                PageInfo<TblStaffMySql> pageInfo = new PageInfo<TblStaffMySql>();
                pageInfo.setCurrentPage(pageNumber);
                pageInfo.setPageSize(pageSize);
                pageInfo.setTlist(tblStaffMySqlMapper.selectListByPage(pageInfo, pid));
                pageInfo.setTotalRecord(tblStaffMySqlMapper.selectListByPageCount(pageInfo, pid));
                resultMap.put("code", "1");
                resultMap.put("msg", "访问接口成功");
                resultMap.put("data", pageInfo);
                //resultMap.put("data",orgid);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return resultMap;
        }
    }

    @Override
    public Map<String, Object> findAllPageBean(Integer pageNumber, Integer pageSize, String token, String staffId) {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            try {
                TblStaffUtil user = DealUserToken.parseUserToken(token);
                BigDecimal orgid = user.getCurrentOrg().getOrgid();
                PageInfo<TblStaff> pageInfo = new PageInfo<TblStaff>();
                pageInfo.setCurrentPage(pageNumber);
                pageInfo.setPageSize(pageSize);

                pageInfo.setTlist(tblStaffMapper.selectListBy(pageInfo, orgid));
                pageInfo.setTotalRecord(tblStaffMapper.selectListByOrgid(pageInfo, orgid));

                resultMap.put("code", "1");
                resultMap.put("msg", "访问接口成功");
                resultMap.put("data", pageInfo);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return resultMap;
        } else {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            try {
                TblStaffUtil user = DealUserToken.parseUserToken(token);
                BigDecimal orgid = user.getCurrentOrg().getOrgid();
                PageInfo<TblStaffMySql> pageInfo = new PageInfo<TblStaffMySql>();
                pageInfo.setCurrentPage(pageNumber);
                pageInfo.setPageSize(pageSize);

                pageInfo.setTlist(tblStaffMySqlMapper.selectListBy(pageInfo, orgid));
                pageInfo.setTotalRecord(tblStaffMySqlMapper.selectListByOrgid(pageInfo, orgid));

                resultMap.put("code", "1");
                resultMap.put("msg", "访问接口成功");
                resultMap.put("data", pageInfo);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return resultMap;
        }

    }

    @Override
    public Map<String, Object> findUserByZbsjForQx(PageInfo<TblStaff> pageInfo) {
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        pageInfo.setTlist(tblStaffMapper.findPageListBySql(pageInfo));
        pageInfo.setTotalRecord(tblStaffMapper.findPageCountBySql(pageInfo));
        resultMap.put("code", "1");
        resultMap.put("msg", "访问接口成功");
        resultMap.put("data", pageInfo);
        return resultMap;
    }

    @Override
    public Map<String, Object> findUserByMySqlZbsjForQx(PageInfo<TblStaffMySql> pageInfo) {
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        pageInfo.setTlist(tblStaffMySqlMapper.findPageListBySql(pageInfo));
        pageInfo.setTotalRecord(tblStaffMySqlMapper.findPageCountBySql(pageInfo));
        resultMap.put("code", "1");
        resultMap.put("msg", "访问接口成功");
        resultMap.put("data", pageInfo);
        return resultMap;
    }

    @Override
    public TblStaff getExpert(BigDecimal staffid) {
        return this.tblStaffMapper.getExpert(staffid);
    }

    @Override
    public TblStaffMySql getMySqlExpert(BigDecimal staffid) {
        return this.tblStaffMySqlMapper.getExpert(staffid);
    }

    @Override
    public void modifyStaff(TblStaff user) {
        tblStaffMapper.updateStaff(user);
    }

    @Override
    public void modifyMySqlStaff(TblStaffMySql user) {
        tblStaffMySqlMapper.updateStaff(user);
    }

    @Override
    public JsonBean findUserInfoByToken(String token) throws Exception {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            log.info("获取用户信息");
            TblStaffUtil staff = DealUserToken.parseUserToken(token);
            if (staff == null) {
                log.info("用户已失效");
                return ResponseFormat.retParam(1, 20006, null);
            }
            log.info("获取用户信息成功");
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            resultMap.put("userInfo", staff);
            return ResponseFormat.retParam(1, 200, resultMap);
        } else {
            log.info("获取用户信息");
            TblStaffUtil staff = DealUserToken.parseUserToken(token);
            if (staff == null) {
                log.info("用户已失效");
                return ResponseFormat.retParam(1, 20006, null);
            }
            log.info("获取用户信息成功");
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            resultMap.put("userInfo", staff);
            return ResponseFormat.retParam(1, 200, resultMap);
        }
    }

    @Override
    public TblStaff findByOragn(String cystaffid) {
        return tblStaffMapper.selectByUserId(cystaffid);
    }

    @Override
    public Map<String, Object> findAllPageBeanPid(String pid, Integer pageNumber, Integer pageSize, String token) {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            try {
                TblStaffUtil user = DealUserToken.parseUserToken(token);
                TblOrganization attribute = new TblOrganization();
                if (pid != null && !pid.equals("")) {
                    attribute = tblOrganizationMapper.findByid(pid);
                } else {
                    TblOrganizationUtil currentOrg = user.getCurrentOrg();
                    attribute.setOrgid(currentOrg.getOrgid());
                    attribute.setOrgtype(currentOrg.getOrgtype());
                }
                PageInfo<TblStaff> pageInfo = new PageInfo<TblStaff>();
                pageInfo.setCurrentPage(pageNumber);
                pageInfo.setPageSize(pageSize);
                pageInfo.setTlist(tblStaffMapper.findAllPageBeanPid(pageInfo, attribute));
                pageInfo.setTotalRecord(tblStaffMapper.findCountPageBeanPid(pageInfo, attribute));
                resultMap.put("code", "1");
                resultMap.put("msg", "访问接口成功");
                resultMap.put("data", pageInfo);

            } catch (Exception e) {
                e.printStackTrace();
            }
            return resultMap;
        } else {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            try {
                TblStaffUtil user = DealUserToken.parseUserToken(token);
                TblOrganizationMySql attribute = new TblOrganizationMySql();
                if (pid != null && !pid.equals("")) {
                    attribute = tblOrganizationMySqlMapper.findByid(pid);
                } else {
                    TblOrganizationUtil currentOrg = user.getCurrentOrg();
                    attribute.setOrgid(currentOrg.getOrgid());
                    attribute.setOrgtype(currentOrg.getOrgtype());
                }
                PageInfo<TblStaffMySql> pageInfo = new PageInfo<>();
                pageInfo.setCurrentPage(pageNumber);
                pageInfo.setPageSize(pageSize);
                pageInfo.setTlist(tblStaffMySqlMapper.findAllPageBeanPid(pageInfo, attribute));
                pageInfo.setTotalRecord(tblStaffMySqlMapper.findCountPageBeanPid(pageInfo, attribute));
                resultMap.put("code", "1");
                resultMap.put("msg", "访问接口成功");
                resultMap.put("data", pageInfo);

            } catch (Exception e) {
                e.printStackTrace();
            }
            return resultMap;
        }
    }

    @Override
    public void updateStaff(TblStaff staff) {
        tblStaffMapper.updateByPrimaryKeySelective(staff);
    }

    @Override
    public void updateMySqlStaff(TblStaffMySql staff) {
        tblStaffMySqlMapper.updateByPrimaryKeySelective(staff);
    }

    @Override
    public List<TblManageRight> findMansgeUserRight(String staffid) {
        return tblStaffMapper.findMansgeUserRight(staffid);
    }

    @Override
    public List<TblManageRightMySql> findMySqlMansgeUserRight(String staffid) {
        return tblStaffMySqlMapper.findMansgeUserRight(staffid);
    }

    @Override
    public List<TblStaff> findByJobid(BigDecimal jobid) {
        return tblStaffMapper.findByJobid(jobid);
    }

    @Override
    public List<TblStaffMySql> findByMySqlJobid(BigDecimal jobid) {
        return tblStaffMySqlMapper.findByJobid(jobid);
    }

    @Override
    public List<BigDecimal> findUserByRole(String string, BigDecimal orgid) {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            // TODO Auto-generated method stub
            return null;
        } else {
            // TODO Auto-generated method stub
            return null;
        }
    }

    @Override
    public TblStaff selectProjectGroupLeader(Integer projectId) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public TblStaffMySql selectMySqlProjectGroupLeader(Integer projectId) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<BigDecimal> findUserByProject(Integer projectId) {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            // TODO Auto-generated method stub
            return null;
        } else {
            // TODO Auto-generated method stub
            return null;
        }
    }

    @Override
    public List<BigDecimal> findTaskControlPerson(Integer projectId, BigDecimal orgid) {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            // TODO Auto-generated method stub
            return null;
        } else {
            // TODO Auto-generated method stub
            return null;
        }
    }

    //	@Override
//	public TblStaff getSTAFFID(BigDecimal userid) {
//
//		return tblStaffMapper.selectStaffId(userid);
//	}
    public JsonBean dealOaLoginSystem(String userName, String verid)
            throws Exception {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            Jedis jedis = null;
            Map<String, Object> datanMap = new HashMap<String, Object>(0);
            try {
                jedis = JedisUtil.getJedis();
                log.info("根据用户名密码 获取用户" + userName + "信息");
                TblStaff staff = this.tblStaffMapper.selectUniqueStaffInfoByname(userName);
                if (staff == null) {
                    log.info(userName + "用户名或密码错误！");
                    return ResponseFormat.retParam(0, 20002, null);
                }

                TblStaffUtil staffUtil = new TblStaffUtil();
                staffUtil.setStaffid(staff.getStaffid());
                staffUtil.setRealname(staff.getRealname());
                staffUtil.setUsername(staff.getUsername());
                staffUtil.setAddress(staff.getAddress());
                staffUtil.setEmail(staff.getEmail());
                staffUtil.setMiblephone(staff.getMiblephone());
                staffUtil.setMemo(staff.getMemo());
                staffUtil.setStatus(staff.getStatus());
                staffUtil.setCreateDate(staff.getCreateDate());
                staffUtil.setJobid(staff.getJobid());
                staffUtil.setOutSideId(staff.getOutSideId());
                staffUtil.setOutSideOpenId(staff.getOutSideOpenId());
                staffUtil.setFixedphone(staff.getFixedphone());
                staffUtil.setRoleIdStrs(staff.getRoleIdStrs());
                staffUtil.setRoleNames(staff.getRoleNames());
                if (staff.getTrole() != null) {
                    TblRoleUtil roleUtil = new TblRoleUtil();
                    roleUtil.setRid(staff.getTrole().getRid());
                    roleUtil.setRname(staff.getTrole().getRname());
                    staffUtil.setTrole(roleUtil);
                }
                if (staff.getLinkDetp() != null) {
                    TblOrganizationUtil deptUtil = new TblOrganizationUtil();
                    deptUtil.setOrgid(staff.getLinkDetp().getOrgid());
                    deptUtil.setOrgname(staff.getLinkDetp().getOrgname());
                    staffUtil.setLinkDetp(deptUtil);
                }

                TblOrganization linkOrg = this.tblOrganizationMapper.selectCompanyInfoByDeptId(staff.getLinkDetp().getOrgid());

                TblOrganizationUtil orgUtil = new TblOrganizationUtil();
                orgUtil.setOrgid(linkOrg.getOrgid());
                orgUtil.setOrgname(linkOrg.getOrgname());
                orgUtil.setFatherorgid(linkOrg.getFatherorgid());
                orgUtil.setOrgnumber(linkOrg.getOrgnumber());
                orgUtil.setOrgmeno(linkOrg.getOrgmeno());
                orgUtil.setMemo(linkOrg.getMemo());
                orgUtil.setIcode(linkOrg.getIcode());
                orgUtil.setOrgtype(linkOrg.getOrgtype());
                orgUtil.setStatus(linkOrg.getStatus());
                orgUtil.setIszy(linkOrg.getIszy());
                staffUtil.setLinkOrg(orgUtil);
                staffUtil.setCurrentOrg(orgUtil);

                TblLoginType logintype = this.tblLogintypeMapper.selectLoginTypeInfo("/");

                if (logintype != null) {
                    TblLoginTypeUtil typeUtil = new TblLoginTypeUtil();
                    typeUtil.setLoginid(logintype.getLoginid());
                    staffUtil.setLoginType(typeUtil);
                }
                String token = EncryptUtil.getInstance().AESencode(staff.getStaffid().toString(), EncryptUtil.DESKEY);

                jedis.set(staff.getStaffid() + JedisUtil.USERINFOKEY, JSONObject.toJSONString(staffUtil));
                datanMap.put("token", token);
                log.info("用户登录成功，返回用户信息、token: " + token);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (verid != null) {
                    jedis.del(verid);
                }
                if (jedis != null) {
                    jedis.close();
                }
            }
            return ResponseFormat.retParam(1, 200, datanMap);
        } else {
            Jedis jedis = null;
            Map<String, Object> datanMap = new HashMap<String, Object>(0);
            try {
                jedis = JedisUtil.getJedis();
                log.info("根据用户名密码 获取用户" + userName + "信息");
                TblStaffMySql staff = this.tblStaffMySqlMapper.selectUniqueStaffInfoByname(userName);
                if (staff == null) {
                    log.info(userName + "用户名或密码错误！");
                    return ResponseFormat.retParam(0, 20002, null);
                }

                TblStaffUtil staffUtil = new TblStaffUtil();
                staffUtil.setStaffid(staff.getStaffid());
                staffUtil.setRealname(staff.getRealname());
                staffUtil.setUsername(staff.getUsername());
                staffUtil.setAddress(staff.getAddress());
                staffUtil.setEmail(staff.getEmail());
                staffUtil.setMiblephone(staff.getMiblephone());
                staffUtil.setMemo(staff.getMemo());
                staffUtil.setStatus(staff.getStatus());
                staffUtil.setCreateDate(staff.getCreateDate());
                staffUtil.setJobid(staff.getJobid());
                staffUtil.setOutSideId(staff.getOutSideId());
                staffUtil.setOutSideOpenId(staff.getOutSideOpenId());
                staffUtil.setFixedphone(staff.getFixedphone());
                staffUtil.setRoleIdStrs(staff.getRoleIdStrs());
                staffUtil.setRoleNames(staff.getRoleNames());
                if (staff.getTrole() != null) {
                    TblRoleUtil roleUtil = new TblRoleUtil();
                    roleUtil.setRid(staff.getTrole().getRid());
                    roleUtil.setRname(staff.getTrole().getRname());
                    staffUtil.setTrole(roleUtil);
                }
                if (staff.getLinkDetp() != null) {
                    TblOrganizationUtil deptUtil = new TblOrganizationUtil();
                    deptUtil.setOrgid(staff.getLinkDetp().getOrgid());
                    deptUtil.setOrgname(staff.getLinkDetp().getOrgname());
                    staffUtil.setLinkDetp(deptUtil);
                }

                TblOrganizationMySql linkOrg = this.tblOrganizationMySqlMapper.selectCompanyInfoByDeptId(staff.getLinkDetp().getOrgid());

                TblOrganizationUtil orgUtil = new TblOrganizationUtil();
                orgUtil.setOrgid(linkOrg.getOrgid());
                orgUtil.setOrgname(linkOrg.getOrgname());
                orgUtil.setFatherorgid(linkOrg.getFatherorgid());
                orgUtil.setOrgnumber(linkOrg.getOrgnumber());
                orgUtil.setOrgmeno(linkOrg.getOrgmeno());
                orgUtil.setMemo(linkOrg.getMemo());
                orgUtil.setIcode(linkOrg.getIcode());
                orgUtil.setOrgtype(linkOrg.getOrgtype());
                orgUtil.setStatus(linkOrg.getStatus());
                orgUtil.setIszy(linkOrg.getIszy());
                staffUtil.setLinkOrg(orgUtil);
                staffUtil.setCurrentOrg(orgUtil);

                TblLoginTypeMySql logintype = this.tblLoginTypeMySqlMapper.selectLoginTypeInfo("/");

                if (logintype != null) {
                    TblLoginTypeUtil typeUtil = new TblLoginTypeUtil();
                    typeUtil.setLoginid(logintype.getLoginid());
                    staffUtil.setLoginType(typeUtil);
                }
                String token = EncryptUtil.getInstance().AESencode(staff.getStaffid().toString(), EncryptUtil.DESKEY);

                jedis.set(staff.getStaffid() + JedisUtil.USERINFOKEY, JSONObject.toJSONString(staffUtil));
                datanMap.put("token", token);
                log.info("用户登录成功，返回用户信息、token: " + token);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (verid != null) {
                    jedis.del(verid);
                }
                if (jedis != null) {
                    jedis.close();
                }
            }
            return ResponseFormat.retParam(1, 200, datanMap);
        }
    }

    @Override
    public JsonBean modifyUserPassWord(String token, Integer staffId, String oldPassWord, String newPassWord, String twoPassWord)
            throws Exception {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            TblStaffUtil user = DealUserToken.parseUserToken(token);
            if (null == user) {
                return ResponseFormat.retParam(0, 10002, null);
            }

            TblStaff staff = this.tblStaffMapper.selectPid(staffId.toString());

            if (staffId == 0 || staffId == null) {
                return ResponseFormat.retParam(0, 10002, null);
            }
            if (oldPassWord == null || "".equals(oldPassWord)) {
                return ResponseFormat.retParam(0, 10002, null);
            }
            if (newPassWord == null || "".equals(newPassWord)) {
                return ResponseFormat.retParam(0, 10002, null);
            }
            if (twoPassWord == null || "".equals(twoPassWord)) {
                return ResponseFormat.retParam(0, 10002, null);
            }
            if (!newPassWord.equals(twoPassWord)) {
                return ResponseFormat.retParam(0, 20008, null);
            }

            //==中文验证
            Pattern p = Pattern.compile("[\u4E00-\u9FA5|\\！|\\，|\\。|\\（|\\）|\\《|\\》|\\“|\\”|\\？|\\：|\\；|\\【|\\】]");
            Matcher m = p.matcher(newPassWord);
            if (m.find()) {
                return ResponseFormat.retParam(0, 20009, null);
            }


            if (!Md5util.encrypByMd5(oldPassWord).equals(staff.getPassword())) {
                return ResponseFormat.retParam(0, 50005, null);
            }
            this.tblStaffMapper.updateStaffPassWord(staffId, Md5util.encrypByMd5(newPassWord));
            return ResponseFormat.retParam(1, 200, null);
        } else {
            TblStaffUtil user = DealUserToken.parseUserToken(token);
            if (null == user) {
                return ResponseFormat.retParam(0, 10002, null);
            }

            TblStaffMySql staff = this.tblStaffMySqlMapper.selectPid(staffId.toString());

            if (staffId == 0 || staffId == null) {
                return ResponseFormat.retParam(0, 10002, null);
            }
            if (oldPassWord == null || "".equals(oldPassWord)) {
                return ResponseFormat.retParam(0, 10002, null);
            }
            if (newPassWord == null || "".equals(newPassWord)) {
                return ResponseFormat.retParam(0, 10002, null);
            }
            if (twoPassWord == null || "".equals(twoPassWord)) {
                return ResponseFormat.retParam(0, 10002, null);
            }
            if (!newPassWord.equals(twoPassWord)) {
                return ResponseFormat.retParam(0, 20008, null);
            }

            //==中文验证
            Pattern p = Pattern.compile("[\u4E00-\u9FA5|\\！|\\，|\\。|\\（|\\）|\\《|\\》|\\“|\\”|\\？|\\：|\\；|\\【|\\】]");
            Matcher m = p.matcher(newPassWord);
            if (m.find()) {
                return ResponseFormat.retParam(0, 20009, null);
            }


            if (!Md5util.encrypByMd5(oldPassWord).equals(staff.getPassword())) {
                return ResponseFormat.retParam(0, 50005, null);
            }
            this.tblStaffMySqlMapper.updateStaffPassWord(staffId, Md5util.encrypByMd5(newPassWord));
            return ResponseFormat.retParam(1, 200, null);
        }
    }

    @Override
    public TblStaffMySql findByMySqlOragn(String cystaffid) {
        return tblStaffMySqlMapper.selectByUserId(cystaffid);
    }

	@Override
	public JsonBean mergeStaffManageInfo(String data, String attids) throws Exception {
		// TODO Auto-generated method stub
		net.sf.json.JSONObject json=net.sf.json.JSONObject.fromObject(data);
      TblStaff tblStaff = (TblStaff) net.sf.json.JSONObject.toBean(json,TblStaff.class);
		  Map<String, Object> resultMap = new HashMap<String, Object>(0);    
		  try {
		if(!StringUtils.isEmpty(tblStaff.getStaffid().toString())){
            	TblStaff user=tblStaffMapper.findById(tblStaff.getStaffid());
            	if(user!=null){
            	user.setIsAudit("Y");
                user.setMiblephone(tblStaff.getMiblephone());
                user.setPoliticaloutlook(tblStaff.getPoliticaloutlook());
                user.setEducation(tblStaff.getEducation());
                user.setMajor(tblStaff.getMajor());
                user.setSchool(tblStaff.getSchool());
                user.setOfficephone(tblStaff.getOfficephone());
                user.setTitle(tblStaff.getTitle());
                user.setQualification(tblStaff.getQualification());
                user.setSituation(tblStaff.getSituation());
                user.setJobexperiences(tblStaff.getJobexperiences());
                user.setMemo(tblStaff.getMemo());
                if(attids != null && !"".equals(attids)) {
        			String[] attId = attids.split(",");
        			for (String aid : attId) {
        				tblStaffMapper.insertAttInfoForStaff(user.getStaffid(),aid);
        			}
        		}
                tblStaffMapper.updateStaff(user);
            	}
		        }
                resultMap.put("code", "1");
                resultMap.put("msg", "修改成功");
		  } catch (Exception e) {
				// TODO: handle exception
			  e.printStackTrace();
			}
            return ResponseFormat.retParam(1, 200, resultMap);

	}

	@Override
	public JsonBean getStaffList(BigDecimal orgid,String token, Integer pageNumber, Integer pageSize, String realname) throws Exception {
		Map<String,Object> resultMap = new HashMap<String,Object>(0);
		try {
			TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
			if(loginStaff == null) {
				return ResponseFormat.retParam(0,20006,null);
			}
	    	if(pageNumber == null) {
	    		pageNumber = 1;
	    	}
	    	if(pageSize==null) {
	    		pageSize=15;
	    	}
	    	PageInfo<TblStaff> pageInfo = new PageInfo<TblStaff>();
	    	pageInfo.setPageSize(pageSize);
	    	pageInfo.setCurrentPage(pageNumber);
	    	pageInfo.setTlist(this.tblStaffMapper.selectStaffListByPageInfo(pageInfo,loginStaff.getCurrentOrg().getOrgid(),realname));
	    	pageInfo.setTotalRecord(this.tblStaffMapper.selectStaffCountByPageInfo(loginStaff.getCurrentOrg().getOrgid(),realname));
	    	pageInfo.getTotalPage();
	    	resultMap.put("pageInfo", pageInfo);
	    	resultMap.put("code", "1");
	         resultMap.put("msg", "操作成功");
		} catch (Exception e) {
			// TODO: handle exception
			resultMap.put("code", "0");
	         resultMap.put("msg", "操作失败");
	         e.printStackTrace();
			return ResponseFormat.retParam(0,30001,resultMap);
		}
    	return ResponseFormat.retParam(1,200,resultMap);
	}

	@Override
	public JsonBean findSjStaffDetail(String token, String staffid) throws Exception {
		// TODO Auto-generated method stub
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
		Map<String,Object> resultMap = new HashMap<String,Object>(0);
		TblStaff staff = this.tblStaffMapper.selectUserId(new BigDecimal(staffid));
        TblOrganization linkOrg = this.tblOrganizationMapper.selectCompanyInfoByDeptId(staff.getOrgid());
        TblOrganization linkdept = this.tblOrganizationMapper.findPid(staff.getOrgid().toString());
        staff.setLinkDetp(linkdept);
        staff.setCurrentOrg(linkOrg);
		List<TblPersonalTrain> train =  this.tblPersonalTrainMapper.findAllTrain(staffid);
		resultMap.put("staff", staff);
		resultMap.put("trainList", train);
		resultMap.put("code", "1");
        resultMap.put("msg", "操作成功");
		return ResponseFormat.retParam(1,200,resultMap);
	}

	@Override
	public JsonBean saveOrUpdateTrain(TblPersonalTrain data, String staffid,String trainattids) throws Exception {
		// TODO Auto-generated method stub
		Map<String,Object> resultMap = new HashMap<String,Object>(0);
		try {
			net.sf.json.JSONObject json=net.sf.json.JSONObject.fromObject(data);
			TblPersonalTrain train = (TblPersonalTrain) net.sf.json.JSONObject.toBean(json,TblPersonalTrain.class);
			if(train.getTrainid()!=null){
				train.setStaffid(staffid);
				tblPersonalTrainMapper.saveTrain(train);
				  if(trainattids != null && !"".equals(trainattids)) {
	        			String[] attId = trainattids.split(",");
	        			for (String aid : attId) {
	        				this.tblStaffMapper.insertAttInfoForTrain(train.getTrainid(),aid);
	        			}
	        		}
			}else{
				train.setStaffid(staffid);
				tblPersonalTrainMapper.updateTrain(train);
			}
			 resultMap.put("code", "1");
	         resultMap.put("msg", "操作成功");
		} catch (Exception e) {
			// TODO: handle exception
			 resultMap.put("code", "0");
	         resultMap.put("msg", "操作失败");
		     return ResponseFormat.retParam(0,30001,resultMap);
		}
		return ResponseFormat.retParam(1,200,resultMap);
	}

	@Override
	public JsonBean deleteTrain(String token,String trainid) throws Exception {
		// TODO Auto-generated method stub
		Map<String,Object> resultMap = new HashMap<String,Object>(0);
		try {
			tblPersonalTrainMapper.deleteTrainById(trainid);
			 resultMap.put("code", "1");
	         resultMap.put("msg", "操作成功");
		} catch (Exception e) {
			// TODO: handle exception
			 resultMap.put("code", "0");
	         resultMap.put("msg", "操作失败");
		     return ResponseFormat.retParam(0,30001,resultMap);
		}
		return ResponseFormat.retParam(1,200,resultMap);
	}

	@Override
	public JsonBean getStaffAttInfo(String token, String staffid) throws Exception {
		// TODO Auto-generated method stub
		TblStaffUtil user = DealUserToken.parseUserToken(token);
		if(user == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
		Map<String,Object> resultMap = new HashMap<String,Object>(0);
		List<TblAttachment> attList = this.tblAttachmentMapper.selectAttListByStaffId(staffid);
		resultMap.put("attList", attList);
		return ResponseFormat.retParam(1,200,attList);
	}

	@Override
	public JsonBean deleStaffAttInfo(String token, String attid) throws Exception {
		// TODO Auto-generated method stub
		Map<String,Object> resultMap = new HashMap<String,Object>(0);
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return ResponseFormat.retParam(0,20006,null);	
		}
	    return deleteRealtionAttInfo(attid,"staff");
	}
	
	@Override
	public JsonBean deleTrainAttInfo(String token, String attid) throws Exception {
		// TODO Auto-generated method stub
		Map<String,Object> resultMap = new HashMap<String,Object>(0);
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return ResponseFormat.retParam(0,20006,null);	
		}
	    return deleteRealtionAttInfo(attid,"train");
	}
	
	
	private JsonBean deleteRealtionAttInfo(String attId,String type) throws Exception {
		boolean b = false;
		Map<String,Object> resultMap = new HashMap<String,Object>(0);
        TblAttachment att = this.tblAttachmentMapper.selectEntityById(attId);
        if (att == null) {
        	resultMap.put("code", "0");
	        resultMap.put("msg", "附件不存在或已经删除");
			return ResponseFormat.retParam(0,50001,null);
        }
        int index = att.getAttpath().lastIndexOf("/") + 1; //右边第一个/
        String path = att.getAttpath().substring(0, index);
        String fileName = att.getAttpath().substring(index);
        try {
            b = FtpUtil.removeFile(fileName, path);
        } catch (Exception e) {
        	resultMap.put("code", "0");
	        resultMap.put("msg", "文件删除失败");
			return ResponseFormat.retParam(0,50001,null);
        }
        if (!b) {
        	resultMap.put("code", "0");
	        resultMap.put("msg", "文件删除失败");
			return ResponseFormat.retParam(0,50001,null);
        }
        this.tblAttachmentMapper.deleteEntity(att.getAttid());
        if(type.equals("staff")){
        	   this.tblStaffMapper.deleteStaffFileInfoByAttId(att.getAttid());
        }else if(type.equals("train")){
        	   this.tblStaffMapper.deleteTrainFileInfoByAttId(att.getAttid());
        }
     
        resultMap.put("code", "1");
        resultMap.put("msg", "文件删除成功");
    	return ResponseFormat.retParam(1,200,resultMap);
	}

	@Override
	public JsonBean getAuditorInformationList(String token, String orgid) throws Exception {
		// TODO Auto-generated method stub
		Map<String,Object> resultMap = new HashMap<String,Object>(0);
		try {
			TblStaffUtil user = DealUserToken.parseUserToken(token);
			if(user == null) {
				return ResponseFormat.retParam(0,20006,null);
			}
			 net.sf.json.JSONObject  data=tblStaffMapper.getAuditorInformationList(orgid);
			resultMap.put("data", data);
		} catch (Exception e) {
			// TODO: handle exception
			return ResponseFormat.retParam(0,5001,resultMap);
		}
		return ResponseFormat.retParam(1,200,resultMap);
	}

	@Override
	public Map<String, Object> syncUser(Integer operaType, String data) throws Exception {
		// TODO Auto-generated method stub
		  Map<String, Object> resultMap = new HashMap<String, Object>(0);
		  SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		  try {
			  net.sf.json.JSONObject object= net.sf.json.JSONObject.fromObject(data);
			  JSONArray arr= JSONArray.fromObject(object.get("dataList"));
			  for(Object o :arr){
				  net.sf.json.JSONObject obj=(net.sf.json.JSONObject)o;
				  List<TblStaff> staffList=tblStaffMapper.getStffListByHistorycode(obj.getString("id"));
					if(staffList.size()==0){
						TblStaff staff=new TblStaff();
						staff.setDataSource("zz");
						staff.setHistorycode(obj.getString("staffid"));
						staff.setHistorydepartmentid(obj.get("orgid").equals("")?"":obj.get("orgid").toString());
						staff.setJobid(obj.get("jobid").equals("")?new BigDecimal(0):new BigDecimal(obj.get("jobid").toString()));
						staff.setRealname(obj.getString("realname"));
						staff.setUsername(obj.get("username").equals("")?"":obj.get("username").toString());
						if(!obj.get("birthday").equals("")){
							staff.setBirthday(sdf.parse(obj.get("birthday").toString()));
						}
						staff.setPoliticaloutlook(obj.get("politicaloutlook").equals("")?"":obj.get("politicaloutlook").toString()); //政治⾯貌
						staff.setAddress(obj.get("address").equals("")?"":obj.get("address").toString());
					    staff.setEducation(obj.get("education").equals("")?"":obj.get("education").toString());
					    staff.setMiblephone(obj.get("miblephone").equals("")?"":obj.get("miblephone").toString());
					    staff.setOfficephone(obj.get("officephone").equals("")?"":obj.get("officephone").toString());
						staff.setEmail(obj.get("email").equals("")?"":obj.get("email").toString());
						staff.setStatus(obj.getString("status").equals("1")?1:0); 
						staff.setType(obj.get("workType").equals("")?"":obj.get("workType").toString());
						if(!obj.get("startTime").equals("")){
							staff.setEntrytime(sdf.parse(obj.get("startTime").toString()));
						}
						if(!obj.get("endTime").equals("")){
							staff.setResignationtime(sdf.parse(obj.get("endTime").toString()));
						}
						staff.setGender(obj.get("sex").toString());
						staff.setTitle(obj.get("title").equals("")?"":obj.get("title").toString());
						staff.setMajor(obj.get("major").equals("")?"":obj.get("major").toString());
						staff.setSchool(obj.get("school").equals("")?"":obj.get("school").toString());
						staff.setQualification(obj.get("qualifications").equals("")?"":obj.get("qualifications").toString());
						staff.setJobexperiences(obj.get("jobexperiences").equals("")?"":obj.get("jobexperiences").toString());
						staff.setSituation(obj.get("situation").equals("")?"":obj.get("situation").toString());
						staff.setMemo(obj.get("memo").equals("")?"":obj.get("memo").toString());
						staff.setRoleIdStrs("12"); //授权为普通用户
						if(!obj.get("worktime").equals("")){
	                        staff.setWorktime(sdf.parse(obj.get("worktime").toString()));
						}
						staff.setPassword(MD5Encrypt.encrypByMd5("1qaz!QAZ"));
						tblStaffMapper.insertUser(staff);
					}else{
						TblStaff staff=staffList.get(0);
						staff.setDataSource("zz");
						staff.setHistorycode(obj.getString("staffid"));
						staff.setHistorydepartmentid(obj.get("orgid").equals("")?"":obj.get("orgid").toString());
						staff.setJobid(obj.get("jobid").equals("")?new BigDecimal(0):new BigDecimal(obj.get("jobid").toString()));
						staff.setRealname(obj.getString("realname"));
						staff.setUsername(obj.get("username").equals("")?"":obj.get("username").toString());
						if(!obj.get("birthday").equals("")){
							staff.setBirthday(sdf.parse(obj.get("birthday").toString()));
						}
						staff.setPoliticaloutlook(obj.get("politicaloutlook").equals("")?"":obj.get("politicaloutlook").toString()); //政治⾯貌
						staff.setAddress(obj.get("address").equals("")?"":obj.get("address").toString());
					    staff.setEducation(obj.get("education").equals("")?"":obj.get("education").toString());
					    staff.setMiblephone(obj.get("miblephone").equals("")?"":obj.get("miblephone").toString());
					    staff.setOfficephone(obj.get("officephone").equals("")?"":obj.get("officephone").toString());
						staff.setEmail(obj.get("email").equals("")?"":obj.get("email").toString());
						staff.setStatus(obj.getString("status").equals("1")?1:0); 
						staff.setType(obj.get("workType").equals("")?"":obj.get("workType").toString());
						if(!obj.get("startTime").equals("")){
							staff.setEntrytime(sdf.parse(obj.get("startTime").toString()));
						}
						if(!obj.get("endTime").equals("")){
							staff.setResignationtime(sdf.parse(obj.get("endTime").toString()));
						}
						staff.setGender(obj.get("sex").toString());
						staff.setTitle(obj.get("title").equals("")?"":obj.get("title").toString());
						staff.setMajor(obj.get("major").equals("")?"":obj.get("major").toString());
						staff.setSchool(obj.get("school").equals("")?"":obj.get("school").toString());
						staff.setQualification(obj.get("qualifications").equals("")?"":obj.get("qualifications").toString());
						staff.setJobexperiences(obj.get("jobexperiences").equals("")?"":obj.get("jobexperiences").toString());
						staff.setSituation(obj.get("situation").equals("")?"":obj.get("situation").toString());
						staff.setMemo(obj.get("memo").equals("")?"":obj.get("memo").toString());
						if(!obj.get("worktime").equals("")){
	                        staff.setWorktime(sdf.parse(obj.get("worktime").toString()));
						}
						tblStaffMapper.updateStaff(staff);
					}
			  }
			    //修改同步人员的岗位id
				tblStaffMapper.updateTblStaffSyncJob();
				//修改同步人员的部门id
				tblStaffMapper.updateTblStaffSyncOrg();
			  resultMap.put("code", "0");
              resultMap.put("msg", "人员同步成功");
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return null;
	}

	@Override
	public void syncPerson(String data) throws Exception {
		// TODO Auto-generated method stub
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		try {
			net.sf.json.JSONObject paramStr = net.sf.json.JSONObject.fromObject(data);
			net.sf.json.JSONObject msg = net.sf.json.JSONObject.fromObject(paramStr.get("msg").toString());
			if(paramStr.get("success").toString()=="true"){
			net.sf.json.JSONArray dataList = net.sf.json.JSONArray.fromObject(msg.get("dataList"));
			for (Object o : dataList) {
				net.sf.json.JSONObject obj=(net.sf.json.JSONObject)o;
				List<TblStaff> staffList=tblStaffMapper.getStffListByHistorycode(obj.getString("id"));
				if(staffList.size()==0){
					TblStaff staff=new TblStaff();
					staff.setDataSource("zz");
					staff.setHistorycode(obj.getString("id"));
					staff.setHistorydepartmentid(obj.get("deptId").equals("")?"":obj.get("deptId").toString());
					staff.setJobid(obj.get("jobId").equals("")?new BigDecimal(0):new BigDecimal(obj.get("jobId").toString()));
					staff.setRealname(obj.getString("name"));
					staff.setUsername(obj.get("id").equals("")?"":obj.get("id").toString());
					if(!obj.get("birthday").equals("")){
						staff.setBirthday(sdf.parse(obj.get("birthday").toString()));
					}
					staff.setPoliticaloutlook(obj.get("politicalType").equals("")?"":obj.get("politicalType").toString()); //政治⾯貌
					staff.setAddress(obj.get("address").equals("")?"":obj.get("address").toString());
				    staff.setEducation(obj.get("educationType").equals("")?"":obj.get("educationType").toString());
				    staff.setMiblephone(obj.get("phone").equals("")?"":obj.get("phone").toString());
				    staff.setOfficephone(obj.get("officePhone").equals("")?"":obj.get("officePhone").toString());
					staff.setEmail(obj.get("email").equals("")?"":obj.get("email").toString());
					staff.setStatus(obj.getString("state").equals("离职")?1:0); 
					staff.setType(obj.get("workType").equals("")?"":obj.get("workType").toString());
					staff.setRoleIdStrs("12"); //授权为普通用户
					if(!obj.get("startTime").equals("")){
						staff.setEntrytime(sdf.parse(obj.get("startTime").toString()));
					}
					if(!obj.get("endTime").equals("")){
						staff.setResignationtime(sdf.parse(obj.get("endTime").toString()));
					}
					staff.setGender(obj.get("gender").equals("男")?"1":"0");
					staff.setTitle(obj.get("jobName").equals("")?"":obj.get("jobName").toString());
					staff.setPassword(MD5Encrypt.encrypByMd5("1qaz!QAZ"));
					tblStaffMapper.insertUser(staff);
				}else{
					TblStaff staff=staffList.get(0);
					staff.setDataSource("zz");
					staff.setHistorycode(obj.getString("id"));
					staff.setHistorydepartmentid(obj.get("deptId").equals("")?"":obj.get("deptId").toString());
					staff.setJobid(obj.get("jobId").equals("")?new BigDecimal(0):new BigDecimal(obj.get("jobId").toString()));
					staff.setRealname(obj.getString("name"));
					staff.setUsername(obj.get("id").equals("")?"":obj.get("id").toString());
					if(!obj.get("birthday").equals("")){
						staff.setBirthday(sdf.parse(obj.get("birthday").toString()));
					}
					staff.setPoliticaloutlook(obj.get("politicalType").equals("")?"":obj.get("politicalType").toString()); //政治⾯貌
					staff.setAddress(obj.get("address").equals("")?"":obj.get("address").toString());
				    staff.setEducation(obj.get("educationType").equals("")?"":obj.get("educationType").toString());
				    staff.setMiblephone(obj.get("phone").equals("")?"":obj.get("phone").toString());
				    staff.setOfficephone(obj.get("officePhone").equals("")?"":obj.get("officePhone").toString());
					staff.setEmail(obj.get("email").equals("")?"":obj.get("email").toString());
					staff.setStatus(obj.getString("state").equals("离职")?1:0); 
					staff.setType(obj.get("workType").equals("")?"":obj.get("workType").toString());
					if(!obj.get("startTime").equals("")){
						staff.setEntrytime(sdf.parse(obj.get("startTime").toString()));
					}
					if(!obj.get("endTime").equals("")){
						staff.setResignationtime(sdf.parse(obj.get("endTime").toString()));
					}
					staff.setGender(obj.get("gender").equals("男")?"1":"0");
					staff.setTitle(obj.get("jobName").equals("")?"":obj.get("jobName").toString());
					tblStaffMapper.updateStaff(staff);
				}
			}
			//修改同步人员的岗位id
			tblStaffMapper.updateTblStaffSyncJob();
			//修改同步人员的部门id
			tblStaffMapper.updateTblStaffSyncOrg();
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
}
